package com.base.core.util;

import com.base.core.type.JsonUtils;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;

public abstract class JsonNodeUtils {

    private static ObjectNode root = JsonUtils.mapper().createObjectNode();

    public static JsonNode getRoot() {
        return root;
    }

    public static void put(String path, JsonNode node) {
        ObjectNode parent = root;
        String[] a = path.split("\\.");
        int i = 0, l = a.length - 1;

        while (i < l) {
            ObjectNode child = (ObjectNode) parent.get(a[i]);
            if (child == null) {
                child = parent.putObject(a[i]);
            }
            parent = child;
            ++i;
        }
        parent.set(a[i], node);
    }

    public static JsonNode get(String path) {
        JsonNode parent = root;
        String[] a = path.split("\\.");
        int i = 0, l = a.length;
        while (i < l) {
            parent = parent.get(a[i]);
            if (parent == null)
                return null;
            ++i;
        }
        return parent;
    }
//
//    @SuppressWarnings("unused")
//    private static ObjectNode enumToObject(Class<? extends EnumWithText> enumType) {
//        return JsonUtils.mapper().createObjectNode();
//    }
    
}
